import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-22
 * Time: 14:13
 */
//两道leetcode在idea调试
public class Test {

    //leetcode 26   //严重怀疑自己的智商
    public static int removeDuplicates(int[] nums){
        //双指针
        int i = 0;
        int j = 1;
        while(j < nums.length){
            // j找到不同于i的位置时就让i的下一个位置等于j位置的数
            if(nums[i] == nums[j]){
                j++;
            }else{
                i++;
                nums[i] = nums[j];
                j++;
            }
        }
        return i+1;
    }
    public static int removeDuplicates1(int[] nums) {
        //先记录nums数组的长度
        int count = nums.length;
        int num = 0;
        //定义前后指针
        int i = 0;
        int j = 1;

        //遍历数组，前后相等移动数组，不相等i++j++
        for(int x:nums){
            if(nums[i] == nums[j]){
                for(int a = i; a < count-1; a++){
                    nums[a] = nums[a+1];
                }
                count--;
                num++;        //记录数组移动次数
            }else{
                i++;
                j++;
            }
        }
        //返回数组长度
        return num-1;
    }

    public static void main2(String[] args) {
        int[] nums = {1,1,2};
        System.out.println(removeDuplicates(nums));
        System.out.println(" ");
    }

    //leetcode 35
    public static int searchInsert(int[] nums, int target) {
        //二分查找
        int i = 0;
        int j = nums.length;
        int mid = 0;
        while(i < j){
            mid = (i+j)/2 +1;
            if(target > nums[mid]){
                i = mid+1;
            }else if(target < nums[mid]){
                j = mid;
            }else{
                return mid;
            }
        }
        return i;
    }
    public static void main1(String[] args) {
        int[] nums = {1,2,4,6,7};
        int target = 3;
        System.out.println(searchInsert(nums,target));
    }
}
